package com.hzw.saas.common.config.exception.handler;

import cn.hutool.core.util.StrUtil;
import com.hzw.saas.common.config.configuration.properties.SaasExceptionProperties;
import com.hzw.saas.common.config.exception.DefaultSaasException;
import com.hzw.saas.common.util.exception.BaseException;
import com.hzw.saas.common.util.exception.BaseRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * 异常处理
 */
@Slf4j
@ControllerAdvice
public class SaasCommonExceptionHandler {

    @Autowired
    private SaasExceptionProperties exceptionProperties;

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ResponseEntity<Object> handleException(Exception e) {
        log.error("error", e);
        String errorMsg = Objects.equals(exceptionProperties.isOutputTrack(), true) ? e.getMessage()
            : StrUtil.isNotBlank(exceptionProperties.getOutputMsg()) ? exceptionProperties.getOutputMsg()
            : "服务器繁忙";
        return ResponseEntity.status(500).body(errorMsg);
    }

    @ResponseBody
    @ExceptionHandler(BaseException.class)
    public ResponseEntity<Object> handleException(BaseException e) {
        if (e.isPrintTrack()) {
            log.warn("error", e);
        } else if (e.isPrintMsg()) {
            log.warn("error: " + e.getMessage());
        }
        return ResponseEntity.status(e.httpStatus()).body(e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(BaseRuntimeException.class)
    public ResponseEntity<Object> handleException(BaseRuntimeException e) {
        if (e.isPrintTrack()) {
            log.warn("error", e);
        } else if (e.isPrintMsg()) {
            log.warn("error: " + e.getMessage());
        }
        return ResponseEntity.status(e.httpStatus()).body(e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(DefaultSaasException.class)
    public ResponseEntity<Object> handleException(DefaultSaasException e) {
        log.warn("error", e);
        return ResponseEntity.status(e.getHttpStatus().value()).body(e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseEntity<Object> handleException(IllegalArgumentException e) {
        log.debug("IllegalArgumentException: " + e.getMessage());
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<Object> handleException(MethodArgumentNotValidException e) {
        log.warn("error", e);

        List<String> errorMsg = new ArrayList<>();
        e.getBindingResult().getAllErrors().forEach(error -> {
            errorMsg.add(error.getDefaultMessage());
        });
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(String.join(",", errorMsg));
    }

    @ResponseBody
    @ExceptionHandler(BeansException.class)
    public ResponseEntity<Object> handleException(BeansException e) {
        log.warn("error", e);
        if (exceptionProperties.isOutputTrack()) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("无效的请求参数: " + e.getMessage());
        }
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("无效的请求参数");
    }
}
